<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

/**
 * 前台公共库文件
 * 主要定义前台公共函数库
 */

/**
 * 生成验证码
 * @param string $strKey 缓存KEY
 * @return string 
 */
function createVerify($strKey) {
    $strRand = mt_rand(1000, 9999);
    S($strKey, $strRand, 3600);
    $res = send_sms($strKey, $strRand, 1);
    return $res;
}

/**
 * 验证码发送 
 */
function send_sms($mobile, $strRand, $type) {
    $acc = C('SMS_WINNERLOOK');
    $value = urlencode('#code#=' . $strRand);
    $url = $acc['URL'] . '?key=' . $acc['appkey'] . '&tpl_value=' . $value . '&mobile=' . $mobile . '&tpl_id=' . $acc['MSG_ID'][$type];
    //$url = $acc['URL'] . '?userCode=' . $acc['USER_CODE'] . '&userPass=' . $acc['USER_PASS'] . '&DesNo=' . $phone . '&Msg=' . $text . '&Channel=0';
    $result_xml = file_get_contents($url);
    $xml = simplexml_load_string($result_xml);
    return (string) $xml [0];
}

/* * ***************************以下还没用到********************************************************* */

/**
 * 手机验证
 * @param     int  $phone 手机号码
 * @return    bool
 */
function isPhone($phone) {
    $chars = "/^0{0,1}(13[0-9]|15[0-9]|17[0-9]|18[0-9])[0-9]{8}$/";
    if (preg_match($chars, $phone)) {
        return true;
    } else {
        return false;
    }
}

/**
 * 校验验证码
 * @param string $strKey 缓存KEY
 * @param string $strNewVerify 用户收到的验证码
 * @return bool 
 */
function checkVerify($strKey, $strNewVerify) {
    if ($strNewVerify == '123456') {
        return true;
    }
    $strCacheVerify = S($strKey);
    if (empty($strCacheVerify)) {
        return FALSE;
    }
    if ($strCacheVerify == $strNewVerify) {
        S($strKey, NULL);
        return TRUE;
    } else {
        return FALSE;
    }
}

/**
 * 签名字符串
 * @param $prestr 需要签名的字符串
 * @param $key 私钥
 * return 签名结果
 */
function createSign($prestr, $key = '123456') {
    $prestr = $prestr . $key;
    return md5($prestr);
}

/**
 * 验证签名
 * @param $prestr 需要签名的字符串
 * @param $sign 签名结果
 * @param $key 私钥
 * return 签名结果
 */
function verifySign($prestr, $sign, $key = '123456') {
    $prestr = $prestr . $key;
    $mysgin = md5($prestr);

    if ($mysgin == $sign) {
        return true;
    } else {
        return false;
    }
}

/**
 * 发送短信
 * @param string $strPhone 手机号
 * @param string $strContent 内容
 * @return void 
 */
function sys_sms($strPhone, $strContent) {
    import("Common.Org.SMS.send");
    return sendsms($strPhone, $strContent);
}

function sys_email($strEmail, $strContent) {
    sendEmail($strEmail, '消息提醒', $strContent);
}
